<?php
class RingBackController extends CpController
{
    public function init(){
        $url = new Url();
        $this->breadcrumbs[] = array('name'=>'QT nhạc chờ','link'=>$url->createUrl("ringBack/index"),'class'=>'');
    }
    
    /*
    * Thêm mới danh mục nhạc chờ
    */
    
    public function actionAddIcon(){
        $this->breadcrumbs[] = array('name'=>'Danh sách nhạc chờ','link'=>"#",'class'=>'active');
        $id = isset($_GET["id"]) ? trim($_GET["id"]):0;    
        $categoryId = isset($_GET["categoryId"]) ? intval($_GET["categoryId"]):0;
        $categoryId = isset($_GET["categoryId"]) ? intval($_GET["categoryId"]):0; 
        $keyword = isset($_GET["keyword"]) ? trim($_GET["keyword"]):"";
        $data_category = ARingCategory::getDataById($id) ;
        
        $this->render(
            "addIcon"
            , array(
                "keyword"=>$keyword
                , "data_category"=>$data_category
            )
        );
    }
    
    public function actionAjaxSaveIcon()
    {
        $time_now = time();
        $create_user = Yii::app()->user->name;
        $picture = isset($_POST["picture"]) ? trim($_POST["picture"]) : "";
        $alias = isset($_POST["alias"]) ? trim($_POST["alias"]) : ""; 
        $title = isset($_POST["title"]) ? trim($_POST["title"]) : "";
        $position = isset($_POST["position"]) ? trim($_POST["position"]):  "";
        
        /* them moi icon */
        $data_category = array(
            "picture"=>array("value"=>$picture,"type"=>2),
            "alias"=>array("value"=>Common::generate_slug($title),"type"=>2),  
            "title"=>array("value"=>$title,"type"=>2),
            "position"=>array("value"=>$position,"type"=>1),    
            "create_date"=>array("value"=>$time_now,"type"=>1),    
            "update_date"=>array("value"=>$time_now,"type"=>1),
        );
        $table = "c_ring_category";
        $categoryId = CommonDB::insertRow($table,$data_category);
        
        if($categoryId >0){
            $act = 1;
            $obj = 2;
            $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$categoryId.',name: '.$title.',alias: '.$alias.'}';
            $result = AAdminLog::insertAdminLog($act,$obj,$content);
            echo 1;exit();
        }else{
            echo "Có lỗi trong quá trình xử lý! Hãy báo với kỹ thuật";exit;
        } 
    }
    
    /*
    * Sửa danh mục nhạc chờ 
    */
    
    public function actionEditCategory()
    {
        $id = isset($_GET["id"]) ? intval($_GET["id"]):0;
          
        $data = ARingCategory::getDataByCategory($id);
        $data_catego = ARingCategory::getAllCategory(); 
        $this->breadcrumbs[] = array('name'=>'Sửa ca sỹ/ nhóm nhạc '.$data["title"],'link'=>'#','class'=>'active');    
        $this->render(
            "editCategory"
            ,array(
                "data"=>$data                
                , "data_catego"=>$data_catego
            )
        );
    }
    
    public function actionAjaxUpdateCategoryRing(){
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        $title = isset($_POST["title"]) ? trim($_POST["title"]):"";
        $picture = isset($_POST["picture"]) ? trim($_POST["picture"]):"";    
        $position = isset($_POST["position"]) ? trim($_POST["position"]):"";    
        $data_ring_cat = ARingCategory::getDataByCategory($id);    
        $data = array(   
            "title"=>array("value"=>$title,"type"=>2),
            "picture"=>array("value"=>$picture,"type"=>2), 
            "id"=>array("value"=>$id,"type"=>1), 
            "position"=>array("value"=>$position,"type"=>1), 
            "update_date"=>array("value"=>time(),"type"=>1), 
        );
        $table = "c_ring_category";
        $result = CommonDB::updateRow($table,$data,array("id"));
        if($result >0){
            $act = 2;
            $obj = 4;
            $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',title: '.$title.'} thành ';
            $content .= ' {id: '.$id.',title: '.$title.'}';
            $result = AAdminLog::insertAdminLog($act,$obj,$content);
            echo 1;exit();
        }else{
            echo "Chưa thay đổi dữ liệu";
        }
    }
    
    /*
    * trang danh sach danh muc nhac cho
    */
    
    public function actionCategory(){
        $row_per_page = 20;$arr_ring_id = array();  
        $this->breadcrumbs[] = array('name'=>'Danh sách nhạc chờ','link'=>"#",'class'=>'active');
        //gán biến
        $keyword = isset($_GET["keyword"]) ? trim($_GET["keyword"]):""; 
        $title = isset($_GET["title"]) ? trim($_GET["title"]):""; 
        $order_download = isset($_GET["order_download"]) ? intval($_GET["order_download"]):0;
        $status = isset($_GET["status"]) ? intval($_GET["status"]):2;
        $type = isset($_GET["type"]) ? intval($_GET["type"]):0;
        $page = isset($_GET["page"]) ? intval($_GET["page"]):1;
        //lấy dữ liệu
        $data_cat = ACategory::getAllData(5);
        $data_gen = ARingBack::getAllDataGen();
        list($max_page,$count,$data) = ARingBack::getSearchCategory($keyword,$type,$status,$page,$row_per_page,$order_download,$title);
        for($i=0;$i<count($data);$i++){
            $arr_ring_id[] = $data[$i]["id"];
            $data[$i]["count_file"] = 0;
        }
        $this->render(
            "category"
            , array(
                "data_cat"=>$data_cat
                ,"max_page"=>$max_page
                ,"count"=>$count
                ,"data"=>$data
                ,"data_gen"=>$data_gen
                ,"order_download"=>$order_download
                ,"keyword"=>$keyword
                ,"type"=>$type
                ,"title"=>$title
                ,"status"=>$status,"page"=>$page
            )
        );
    }
    
    /*
    * Sửa trạng thái danh mục nhacjc hờ 
    */
    
    public function actionAjaxCategoryUpdate()
    {
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0;
        if($id >0){
            $data_ca = ARingCategory::getDataById($id);
            $result = AData::updateStatusByDataId($id,$status);
            $data = array(
                "id"=>array("value"=>$id,"type"=>1),
                "status"=>array("value"=>$status,"type"=>1)
            );
            $table = "c_ring_category";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                $act = 2;
                $obj = 4;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',} thành ';
                $content .= ' {id: '.$id.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }             
    }
    
    /************************************************************************************************
    * phần ca sỹ nhóm nhạc
    * 
    * 
    **************************************************************************************************/
    
    /*
    * Trang danh sách ca sỹ nhóm nhạc
    */
    
    public function actionSinger(){
        $row_per_page = 20;
        $arr_ring_id = array();  
        $this->breadcrumbs[] = array('name'=>'Danh sách nhạc chờ','link'=>"#",'class'=>'active');
        //gán biến
        $keyword = isset($_GET["keyword"]) ? trim($_GET["keyword"]):""; 
        $name = isset($_GET["name"]) ? trim($_GET["name"]):"";  
        $order_download = isset($_GET["order_download"]) ? intval($_GET["order_download"]):0;
        $status = isset($_GET["status"]) ? intval($_GET["status"]):2;
        $type = isset($_GET["type"]) ? intval($_GET["type"]):0;
        $page = isset($_GET["page"]) ? intval($_GET["page"]):1;  
        //lấy dữ liệu
        $data_cat = ACategory::getAllData(5);
        $data_gen = ARingBack::getAllDataGen();
        list($max_page,$count,$data) = ARingBack::getDataSearchSinger($keyword,$type,$status,$page,$row_per_page,$order_download,$name);
        for($i=0;$i<count($data);$i++){
            $arr_ring_id[] = $data[$i]["id"];
            $data[$i]["count_file"] = 0;
        }
        $this->render(
            "singer"
            , array(
                "data_cat"=>$data_cat
                , "max_page"=>$max_page
                , "count"=>$count
                , "data"=>$data
                , "data_gen"=>$data_gen
                , "order_download"=>$order_download
                , "keyword"=>$keyword
                , "type"=>$type
                , "name"=>$name
                , "status"=>$status
                , "page"=>$page
            )
        );
    }
    
    /*
    * Trang thêm mới ca sỹ
    */
    
    public function actionAjaxSaveSinger(){
        $time_now = time(); 
        $id = isset($_POST["id"]) ? intval($_POST["id"]) : 0;   
        $name = isset($_POST["name"]) ? trim($_POST["name"]) : "";
        $name_search = Common::generate_slug_search($name);
        $status = isset($_POST["status"]) ? intval($_POST["status"]) : 1;
        $checkSinger = ARingBackSinger::CheckSinger($name);
        
        if($checkSinger > 0){
            echo "Đã tồn tại ca sỹ này";
            exit;
        }
        $data = array(
            "id"=>array("value"=>$id,"type"=>1), 
            "name"=>array("value"=>$name,"type"=>2),
            "name_search"=>array("value"=>$name_search,"type"=>2),
            "status"=>array("value"=>$status,"type"=>1),         
            "create_date"=>array("value"=>time(),"type"=>1),         
            "update_date"=>array("value"=>time(),"type"=>1),         
        );                 
        $table = "c_ring_singer";
        $singerId = CommonDB::insertRow($table,$data);
        
        if($singerId >0){
            $act = 1;
            $obj = 4;
            $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',name: '.$data["name"].'}';
            $result = AAdminLog::insertAdminLog($act,$obj,$content);
            echo 1;
        }else{
            echo "Có lỗi trong quá trình xử lý! Hãy báo với kỹ thuật";exit;
        }
    }
    
    /*
    * Trang sửa tên ca sỹ nhóm nhạc
    */
    
    public function actionEditSinger()
    {
        $id = isset($_GET["id"]) ? intval($_GET["id"]) : 0;
        $singerId = isset($_GET["singerId"]) ? intval($_GET["singerId"]) : 0; 
        $singer = isset($_GET["name"]) ? trim($_GET["name"]) : "";  
        
        $data = ARingBackSinger::getDataBySinger($id);
        $data_singer = ARingBackSinger::getAllData(); 
        $this->breadcrumbs[] = array('name'=>'Sửa ca sỹ/ nhóm nhạc '.$data["name"],'link'=>'#','class'=>'active');
        $this->render(
            "editSinger"
            , array(
                "data"=>$data
                , "singerId"=>$singerId
                , "singer"=>$singer
                , "data_singer"=>$data_singer
            )
        );
    }
    
    public function actionAjaxUpdateSingerRing()
    {
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]) : 0;
        $name = isset($_POST["name"]) ? trim($_POST["name"]) : "";  
        $name_search = Common::generate_slug_search($name);
        $data_ring_back = ARingBackSinger::getDataBySinger($id);    
        
        $checkSinger = ARingBackSinger::CheckSingerEdit($name, $id);
        if($checkSinger > 0){
            echo "Đã tồn tại ca sỹ này";
            exit;
        }
        
        $data = array(   
            "name"=>array("value"=>$name, "type"=>2),
            "name_search"=>array("value"=>$name_search, "type"=>2),
            "update_date"=>array("value"=>time(), "type"=>2),
            "id"=>array("value"=>$id, "type"=>1), 
        );
        
        $table = "c_ring_singer";
        $result = CommonDB::updateRow($table, $data, array("id"));
        if($result >0){
            $act = 2;
            $obj = 4;
            $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',name: '.$data["name"].'} thành ';
            $content .= ' {id: '.$id.',name: '.$name.'}';
            $result = AAdminLog::insertAdminLog($act,$obj,$content);
            echo 1; exit();
        } else {
            echo "Chưa thay đổi dữ liệu";
        }
    }
    
    /*
    * Sửa trạng thái nhạc chờ
    */  
    
    public function actionAjaxSingerUpdate()
    {
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0; 
        if($id >0){
            $data_e = ARingBackSinger::getDataById($id);
            $result = AData::updateStatusByDataId($id,$status);
            $data = array(
                "status"=>array("value"=>$status, "type"=>1),
                "id"=>array("value"=>$id, "type"=>1)
            );
            $table = "c_ring_singer";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                $act = 2;
                $obj = 4;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.'} thành ';
                $content .= ' {id: '.$id.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }             
    }
    
    /************************************************************************************************
    * Phần nhạc chờ
    *************************************************************************************************/
    
    public function actionIndex()
    {
        $row_per_page = 20;
        $arr_ring_id = array();  
        $this->breadcrumbs[] = array('name'=>'Danh sách nhạc chờ','link'=>"#",'class'=>'active');
        //gán biến       
        $keyword = isset($_GET["keyword"]) ? trim($_GET["keyword"]):""; 
        $singerId = isset($_GET["singerId"]) ? intval($_GET["singerId"]):0;
        $ringId = isset($_GET["ringId"]) ? intval($_POST["ringId"]):0;
        $id = isset($_GET["id"]) ? intval($_GET["id"]):0;
        $categoryId = isset($_GET["categoryId"]) ? intval($_GET["categoryId"]):0;
        $type = isset($_GET["type"]) ? intval($_GET["type"]):0;  
        $status = isset($_GET["status"]) ? intval($_GET["status"]):2;
        $lyrics = isset($_GET["lyrics"]) ? intval($_GET["lyrics"]):2;
        $hit = isset($_GET["hit"]) ? intval($_GET["hit"]):2;
        $order_download = isset($_GET["order_download"]) ? intval($_GET["order_download"]):0; 
        $page = isset($_GET["page"]) ? intval($_GET["page"]):1;
        //lấy dữ liệu
        $data_gen = ARingBack::getAllDataGen();
        $data_categoryId = array();
        $data_categoryId = ARingCategory::getAllData();
        //$data_code = array();
        $data_code = ARingCode::getDataCode($ringId); 
        $data_singId=array();
        $data_singId = ARingBackSinger::getDataById($singerId);
        $telco=LoadConfig::$telco;
        list($max_page,$count,$data) = ARingBack::getDataSearch($keyword, $type, $status,$hit,$lyrics,$page,$row_per_page,$order_download,$type,$singerId,$categoryId,$data_categoryId,$data_code,$ringId);
        for($i=0;$i<count($data);$i++){                  
            $arr_ring_id[] = $data[$i]["id"];
            $data[$i]["count_file"] = 0;
        }
        $this->render(
            "index"
            , array(
                "ringId"=>$ringId
                , "data_code"=>$data_code
                , "max_page"=>$max_page
                , "count"=>$count
                , "categoryId"=>$categoryId
                , "data"=>$data
                , "data_gen"=>$data_gen
                , "lyrics"=>$lyrics
                , "order_download"=>$order_download
                , "hit"=>$hit
                , "keyword"=>$keyword
                , "type"=>$type
                , "singerId"=>$singerId
                , "data_singId"=>$data_singId
                , "data_categoryId"=>$data_categoryId
                , "status"=>$status
                , "page"=>$page
            )
        );
    }
    
    /*
    * Thêm mới nhạc chờ
    */
    
    public function actionCreate()
    {
        $this->breadcrumbs[] = array('name'=>'Thêm mới nhạc chờ','link'=>'#','class'=>'active');
        // gán biến
        $id = isset($_GET["id"]) ? intval($_GET["id"]) : 0;
        $codeId = isset($_GET["ringId"]) ? intval($_GET["ringId"]) : 0;   
        $keyword = isset($_GET["keyword"]) ? trim($_GET["keyword"]) : "";
        $singerId = isset($_GET["singerId"]) ? intval($_GET["singerId"]) : 0;
        $categoryId = isset($_GET["categoryId"]) ? intval($_GET["categoryId"]) : "";
        $name = isset($_GET["name"]) ? trim($_GET["name"]) : "";
        $singer_name = isset($_GET["singer_name"]) ? trim($_GET["singer_name"]):""; 
        //lấy dữ liệu
        $data_code = ARingBack::getDataCodeId($codeId); 
        $data_gen = ARingBack::getAllDataGen();
        $data_categoryId = array(); 
        $data_categoryId = ARingCategory::getAllData();
        $data_ringId = ARingBackSinger::getDataById($singerId);
        $data_name = ARingBackSinger::getAllSinger();
        $str="";
        foreach($data_name as $value){
            $str .= "['".$value['name']."','".$value['id']."']," ;   
        }  
        $str = rtrim($str,",");
        $this->render(
            "create"
            ,array(
                "data_code"=>$data_code 
                , "data_gen"=>$data_gen
                , "keyword"=>$keyword
                , "name"=>$name
                , "singer_name"=>$singer_name
                , "str"=>$str
                , "singerId"=>$singerId
                , "data_ringId"=>$data_ringId
                , "categoryId"=>$categoryId
                , "data_categoryId"=>$data_categoryId
            )
        );
    }
    
    public function actionAjaxSaveRing()
    {
        $time_now = time();
        $codeId = isset($_POST["ringId"]) ? intval($_POST["ringId"]):0;     
        $title_ring = isset($_POST["title_ring"]) ? trim(strip_tags($_POST["title_ring"])):"";
        $title_search = Common::removeSign($title_ring);
        $singerId = isset($_POST["singerId"]) ? intval($_POST["singerId"]):0;
        $singer_name = isset($_POST["singer_name"]) ? trim(strip_tags($_POST["singer_name"])) : "";
        $singer_name_search = Common::removeSign($singer_name);
        $categoryId = isset($_POST["categoryId"]) ? intval($_POST["categoryId"]):0;  
        $file = isset($_POST["file"]) ? trim($_POST["file"]):"";
        $lyrics = isset($_POST["lyrics"]) ? trim($_POST["lyrics"]):"";
        $status = isset($_POST["status"]) ? trim($_POST["status"]):"";
        $introtext = isset($_POST["introtext"]) ? trim(strip_tags($_POST["introtext"])):"";
        
        if($title_ring==''){ 
            echo "Tên bài hát không được để trống";
            exit;
        }
        
        if($singer_name==''){ 
            echo "Tên ca sỹ không được để trống";
            exit;
        }
        
        if($categoryId==''){ 
            echo "Tên danh mục không được để trống";
            exit;
        }

        $data_code = ARingBack::getDataCodeId($codeId);
        $data_singId = ARingBackSinger::getDataById($singerId);                       
        $data = array(
            "singerId"=>array("value"=>$singerId,"type"=>1),
            "singer_name"=>array("value"=>$singer_name,"type"=>2),
            "singer_name_search"=>array("value"=>$singer_name_search,"type"=>2),
            "categoryId"=>array("value"=>$categoryId,"type"=>2),
            "title_search"=>array("value"=>$title_search,"type"=>2),
            "title"=>array("value"=>$title_ring,"type"=>2),
            "file"=>array("value"=>$file,"type"=>2),
            "lyrics"=>array("value"=>$lyrics,"type"=>2),    
            "status"=>array("value"=>$status,"type"=>1),    
            "hit"=>array("value"=>0,"type"=>1),     
            "download"=>array("value"=>0,"type"=>1),     
            "create_date"=>array("value"=>$time_now,"type"=>1),     
            "update_date"=>array("value"=>$time_now,"type"=>1),     
        );            
        $table = "c_ring_back";
        $ringId = CommonDB::insertRow($table,$data);  
      
        $data_code = array();
        /* Lấy mã nhạc chờ */
        if(!empty($_POST["telco1"])){
            $viettel = trim(strip_tags($_POST["telco1"]));
            $viettel = explode(";", $viettel);
            
            foreach($viettel as $code){
                $codes["ringId"] = $ringId;
                $codes["telco"] = 1; 
                $codes["code"] = preg_replace('/[^a-zA-Z0-9_ \-()\/%-&]/s', '', $code);
                $codes["deadline"] = "2013-12-30 23:59:59";
                $data_code[] = $codes;
            }
        }
        
        if(!empty($_POST["telco2"])){
            $mobifone = trim(strip_tags($_POST["telco2"]));
            $mobifone = explode(";", $mobifone);
            
            foreach($mobifone as $code){
                $codes["ringId"] = $ringId;
                $codes["telco"] = 2;
                $codes["code"] = preg_replace('/[^a-zA-Z0-9_ \-()\/%-&]/s', '', $code);
                $codes["deadline"] = "2013-12-30 23:59:59";
                $data_code[] = $codes;
            }
        }
        
        if(!empty($_POST["telco3"])){
            $vinaphone = trim(strip_tags($_POST["telco3"]));
            $vinaphone = explode(";", $vinaphone);
            
            foreach($vinaphone as $code){
                $codes["ringId"] = $ringId;
                $codes["telco"] = 3;
                $codes["code"] = preg_replace('/[^a-zA-Z0-9_ \-()\/%-&]/s', '', $code);
                $codes["deadline"] = "2013-12-30 23:59:59";
                $data_code[] = $codes;
            }
        }
        
        if(!empty($_POST["telco4"])){
            $vietnammobile = trim(strip_tags($_POST["telco4"]));
            $vietnammobile = explode(";", $vietnammobile);
            
            foreach($vietnammobile as $code){
                $codes["ringId"] = $ringId;
                $codes["telco"] = 4;
                $codes["code"] = preg_replace('/[^a-zA-Z0-9_ \-()\/%-&]/s', '', $code);
                $codes["deadline"] = "2013-12-30 23:59:59";
                $data_code[] = $codes; 
            }
        }
        
        if(!empty($_POST["telco5"])){
            $beeline = trim(strip_tags($_POST["telco5"]));
            $beeline = explode(";", $beeline);
            
            foreach($beeline as $code){
                $codes["ringId"] = $ringId;
                $codes["telco"] = 5;
                $codes["code"] = preg_replace('/[^a-zA-Z0-9_ \-()\/%-&]/s', '', $code);
                $codes["deadline"] = "2013-12-30 23:59:59";
                $data_code[] = $codes; 
            }
        }        
           
        $result = ARingCode::InsertCode($data_code);
        
        if($ringId >0){
            $act = 1;
            $obj = 4;
            $content = LogConfig::$action[$act] . ' ' . LogConfig::$object[$obj].': {id: '.$ringId.',name: '.$title_ring.',singerId: '.$singerId.'}';
            $result = AAdminLog::insertAdminLog($act, $obj, $content);                
            echo 1;exit();
        }else{
            echo "Có lỗi trong quá trình xử lý! Hãy báo với kỹ thuật";exit;
        }
    }
    
    /*
    * Sửa nhac chờ
    */
    
    public function actionEdit(){
        $id = isset($_GET["id"]) ? intval($_GET["id"]):0;
        
        $type = isset($_GET["type"]) ? intval($_GET["type"]) : 0; 
        $data_code = ARingCode::getDataCode($id); 
        $data = ARingBack::getDataById($id);  
        $data_singer = ARingBackSinger::getAllData();    
        $data_category = ARingCategory::getAllCategory();  
        $this->breadcrumbs[] = array('name'=>'Sửa nhạc chờ '.$data["title"],'link'=>'#','class'=>'active');     
        
        $data_name = ARingBackSinger::getAllSinger();
        $str="";
        foreach($data_name as $value){
            $str .= "['".$value['name']."','".$value['id']."']," ;   
        }  
        $str = rtrim($str, ",");
        
        $viettel = "";
        $mobifone = "";
        $vinaphone = "";
        $vietnammobile = "";
        $beeline = "";
        
        if(!empty($data_code)){
            foreach($data_code[$id] as $telco=>$row){
                if($telco==1 && !empty($row)){
                    foreach($row as $value){
                        $viettel .= ";" . $value;    
                    }
                }
                
                if($telco==2 && !empty($row)){
                    foreach($row as $value){
                        $mobifone .= ";" . $value;    
                    }
                }
                
                if($telco==3 && !empty($row)){
                    foreach($row as $value){
                        $vinaphone .= ";" . $value;    
                    }
                }
                
                if($telco==4 && !empty($row)){
                    foreach($row as $value){
                        $vietnammobile .= ";" . $value;    
                    }
                }
                
                if($telco==5 && !empty($row)){
                    foreach($row as $value){
                        $beeline .= ";" . $value;    
                    }
                }   
            }
            
            $viettel = ltrim($viettel, ";");
            $mobifone = ltrim($mobifone, ";");
            $vinaphone = ltrim($vinaphone, ";");
            $vietnammobile = ltrim($vietnammobile, ";");
            $beeline = ltrim($beeline, ";");    
        }
                
        $this->render(
            "edit"
            , array(                
                "str"=>$str
                , "data"=>$data
                , "data_code"=>$data_code                
                , "type"=>$type
                , "data_singer"=>$data_singer
                , "data_category"=>$data_category
                , "viettel"=>$viettel
                , "mobifone"=>$mobifone
                , "vinaphone"=>$vinaphone
                , "vietnammobile"=>$vietnammobile
                , "beeline"=>$beeline
            )
        );
        
    }
    

    public function actionAjaxUpdateRing(){
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        $ringId = isset($_POST["ringId"]) ? intval($_POST["ringId"]):0;  
        $type = isset($_GET["type"]) ? intval($_GET["type"]):0; 
        $title_ring = isset($_POST["title_ring"]) ? trim(strip_tags($_POST["title_ring"])):"";
        $title_search = Common::removeSign($title_ring);
        $code = isset($_POST["code"]) ? intval($_POST["code"]):0;
        $telco = isset($_POST["telco"]) ? intval($_POST["telco"]):""; 
        $singerId = isset($_POST["singerId"]) ? intval($_POST["singerId"]):0;   
        $singer_name = isset($_POST["singer_name"]) ? trim(strip_tags($_POST["singer_name"])) : "";
        $singer_name_search = Common::removeSign($singer_name);
        
        $file = isset($_POST["file"]) ? trim($_POST["file"]):"";  
        $categoryId = isset($_POST["categoryId"]) ? intval($_POST["categoryId"]):"";     
        $lyrics = isset($_POST["lyrics"]) ? trim($_POST["lyrics"]) : "";
        $status = isset($_POST["status"]) ? intval($_POST["status"]) : 0;
        
        if($title_ring==''){ 
            echo "Tên bài hát không được để trống";
            exit;
        }
        
        if($singer_name==''){ 
            echo "Tên ca sỹ không được để trống";
            exit;
        }
        
        if($categoryId==''){ 
            echo "Tên danh mục không được để trống";
            exit;
        }
        
        $data_ring = ARingBack::getDataById($id);
        $data_t = ARingBack::getAllRingBack($id);
        $data_code = ARingBack::getDataCodeId($ringId);
        if($data_ring){
            $data = array(   
                "title"=>array("value"=>$title_ring,"type"=>2), 
                "singerId"=>array("value"=>$singerId,"type"=>1), 
                "title_search"=>array("value"=>$title_search,"type"=>2),
                "file"=>array("value"=>$file,"type"=>2),
                "lyrics"=>array("value"=>$lyrics,"type"=>2),
                "singer_name"=>array("value"=>$singer_name,"type"=>2),
                "singer_name_search"=>array("value"=>$singer_name_search,"type"=>2),
                "categoryId"=>array("value"=>$categoryId,"type"=>1),
                "update_date"=>array("value"=>$time_now,"type"=>1),
                "status"=>array("value"=>$status,"type"=>1),
                "id"=>array("value"=>$id,"type"=>1),
            );
            $table = "c_ring_back";
            $result = CommonDB::updateRow($table,$data,array("id")); 
            
            /* Lấy mã nhạc chờ */
            $data_code = array();
            if(!empty($_POST["telco1"])){
                $viettel = trim(strip_tags($_POST["telco1"]));
                $viettel = explode(",", $viettel);
                
                foreach($viettel as $code){
                    $codes["ringId"] = $id;
                    $codes["telco"] = 1; 
                    $codes["code"] = preg_replace('/[^a-zA-Z0-9_ \-()\/%-&]/s', '', $code);
                    $data_code[] = $codes;
                }
            }
            
            if(!empty($_POST["telco2"])){
                $mobifone = trim(strip_tags($_POST["telco2"]));
                $mobifone = explode(",", $mobifone);
                
                foreach($mobifone as $code){
                    $codes["ringId"] = $id;
                    $codes["telco"] = 2;
                    $codes["code"] = preg_replace('/[^a-zA-Z0-9_ \-()\/%-&]/s', '', $code);
                    $data_code[] = $codes;
                }
            }
            
            if(!empty($_POST["telco3"])){
                $vinaphone = trim(strip_tags($_POST["telco3"]));
                $vinaphone = explode(",", $vinaphone);

                foreach($vinaphone as $code){
                    $codes["ringId"] = $id;
                    $codes["telco"] = 3;
                    $codes["code"] = preg_replace('/[^a-zA-Z0-9_ \-()\/%-&]/s', '', $code);
                    $data_code[] = $codes;
                }
            }
            
            if(!empty($_POST["telco4"])){
                $vietnammobile = trim(strip_tags($_POST["telco4"]));
                $vietnammobile = explode(",", $vietnammobile);

                foreach($vietnammobile as $code){
                    $codes["ringId"] = $id;
                    $codes["telco"] = 4;
                    $codes["code"] = preg_replace('/[^a-zA-Z0-9_ \-()\/%-&]/s', '', $code);
                    $data_code[] = $codes; 
                }
            }
            
            if(!empty($_POST["telco5"])){
                $beeline = trim(strip_tags($_POST["telco5"]));
                $beeline = explode(",", $beeline);

                foreach($beeline as $code){
                    $codes["ringId"] = $id;
                    $codes["telco"] = 5;
                    $codes["code"] = preg_replace('/[^a-zA-Z0-9_ \-()\/%-&]/s', '', $code);
                    $data_code[] = $codes; 
                }
            }
                 
            $result = ARingCode::UpdateCode($data_code, $id);  
        }
        echo 1;exit();  

    }
    
    /*
    * Sửa trạng thái nhạc chờ 
    */
    
    public function actionAjaxQuickUpdate()
    {
        $time_now = time();
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0;
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            $data_old = ARingBack::getDataById($id);
            $result = AData::updateStatusByDataId($id,$status);
            $data = array(
                "status"=>array("value"=>$status,"type"=>1),
                "id"=>array("value"=>$id,"type"=>1)
            );
            $table = "c_ring_back";
            $result = CommonDB::updateRow($table, $data,array("id"));
            if($result >0){
                $act = 2;
                $obj = 4;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {singerId: '.$id.',name: '.$data_old["title"].',status: '.$data_old["status"].'} thành';
                $content .= ' {singerId: '.$id.',name: '.$data_old["title"].',status: '.$status.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    
    /*************************************************************************************************/
    
    public function actionAjaxDelete(){
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        $ringId = isset($_POST["ringId"]) ? intval($_POST["ringId"]):0;   
        if($id >0){
            $data_code = ARingCode::getDataCode($ringId);
            $table = "c_ring_code"; 
            $result = CommonDB::deleteRow($table,array("ringId"=>$id));  
            $data_rin = ARingBack::getDataById($id);
            
            $table = "c_ring_back";
            $result = CommonDB::deleteRow($table,array("id"=>$id));
            if($result >0){
                $act = 3;
                $obj = 4;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',name: '.$data_rin["title"].'}';    
                $result = AAdminLog::insertAdminLog($act,$obj,$content);                                                
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
                        
    public function actionAjaxSingerDelete(){
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            $data_singer = ARingBackSinger::getDataBySinger($id);
            
            $table = "c_ring_singer";
            $result = CommonDB::deleteRow($table,array("id"=>$id));
            if($result >0){
                $act = 3;
                $obj = 4;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',name: '.$data_singer["name"].'}';      
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }

    public function actionAjaxCategoryDelete(){
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            $data_category = ARingCategory::getDataById($id);            
            $table = "c_ring_category";
            $result = CommonDB::deleteRow($table,array("id"=>$id));
            if($result >0){
                $act = 3;
                $obj = 4;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.'}'; 
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
}
?>
